package com.example.xuegengwang.xuegengwang.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.example.xuegengwang.xuegengwang.bean.MainIndexBean;
import com.example.xuegengwang.xuegengwang.bean.MainPageVolunteerBean;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import view.MyImageLoader;
import volunteer.activity.Volunteer;

/**
 * Created by admin on 2017/2/15.
 */

public class MainPageListViewAdapter extends BaseAdapter {
    private ArrayList<MainIndexBean.ExpertBean> arrayList;
    private LayoutInflater layoutInflater;
    private Context context;


    public MainPageListViewAdapter(Context context, ArrayList<MainIndexBean.ExpertBean> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = layoutInflater.inflate(R.layout.main_page_listview_item, null);
            myHolder.icon = (RoundedImageView) convertView.findViewById(R.id.main_page_listView_item_icon);
            myHolder.name_tv = (TextView) convertView.findViewById(R.id.main_page_listView_item_name);
            myHolder.title_tv = (TextView) convertView.findViewById(R.id.main_page_listView_item_title);
            myHolder.more_tv = (TextView) convertView.findViewById(R.id.main_page_listView_item_more);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        MyImageLoader.showImage(arrayList.get(position).getHeadimgurl(), myHolder.icon);
        myHolder.name_tv.setText(arrayList.get(position).getName());
        myHolder.title_tv.setText(arrayList.get(position).getBrief());
        String more = arrayList.get(position).getAnswered() + "个回答，" + arrayList.get(position).getComment() + "个评价";
        myHolder.more_tv.setText(more);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Volunteer.class);
                intent.putExtra("id", arrayList.get(position).getId());
                intent.putExtra("type", arrayList.get(position).getType());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class MyHolder {
        RoundedImageView icon;
        TextView name_tv, title_tv, more_tv;
    }
}
